package com.zhenyi.xiaohuoche.admin;

import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.SqlPara;
import com.xhc.common.model.XhcAdv;
import com.zhenyi.xhc.core.XHCAuthInterceptor;
import com.zhenyi.xhc.core.XHCController;

@Before(XHCAuthInterceptor.class)
public class AdminAdvController extends XHCController {

	/**
	 * 广告列表 type:1,2
	 */
	@Override
	public void list() {
		Integer type = getParaToInt("type", 1);
		SqlPara sqlPara = Db.getSqlPara("AdvList", type);
		List<XhcAdv> advs = XhcAdv.dao.find(sqlPara);
		renderResult(advs);
	}

	/**
	 * 包括封面,和点击进去的图片组images
	 */
	@Override
	public void submit() {
		String title = getPara("title");
		String image = getPara("image");
		String images = getPara("images");
		Integer type = getParaToInt("type");
		XhcAdv adv = new XhcAdv();
		adv.setTitle(title);
		adv.setImage(image);
		adv.setImages(images);
		adv.setType(type);
		boolean flag = adv.save();
		renderFlag(flag);
	}

	@Override
	public void delete() {
		Long id = getParaToLong("id");
		if (id == null) {
			renderFailure("id is null");
			return;
		}
		XhcAdv adv = XhcAdv.dao.findById(id);
		if (adv == null) {
			renderFailure("adv is null");
			return;
		}
		Boolean flag = adv.delete();
		if (flag) {
			renderSuccess();
		} else {
			renderMsg("删除失败");
		}
	}
}
